package com.example.lenovo.newgift;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;

import com.example.lenovo.newgift.category.CategoryFragment;
import com.example.lenovo.newgift.home.HomeFragment;
import com.example.lenovo.newgift.profile.ProfileFragment;
import com.example.lenovo.newgift.rank.RankFragment;

public class MainActivity extends FragmentActivity implements View.OnClickListener {

    private View mTabHome;
    private View mTabRank;
    private View mTabCategory;
    private View mTabProfile;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView () {
        mTabHome = findViewById(R.id.tab_home);
        mTabRank = findViewById(R.id.tab_rank);
        mTabCategory = findViewById(R.id.tab_category);
        mTabProfile = findViewById(R.id.tab_profile);

        mTabHome.setOnClickListener(this);
        mTabRank.setOnClickListener(this);
        mTabCategory.setOnClickListener(this);
        mTabProfile.setOnClickListener(this);
        mTabHome.performClick();
    }

    @Override
    public void onClick (View v) {
        resetTabStatus();
        v.setSelected(true);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        switch (v.getId()) {
            case R.id.tab_home:
                ft.replace(R.id.fragment_content, new HomeFragment());
                break;
            case R.id.tab_rank:
                ft.replace(R.id.fragment_content, new RankFragment());
                break;
            case R.id.tab_category:
                ft.replace(R.id.fragment_content, new CategoryFragment());
                break;
            case R.id.tab_profile:
                ft.replace(R.id.fragment_content, new ProfileFragment());
                break;
        }
        ft.commit();
    }

    private void resetTabStatus(){
        mTabHome.setSelected(false);
        mTabRank.setSelected(false);
        mTabCategory.setSelected(false);
        mTabProfile.setSelected(false);
    }
}
